# -*- coding: utf-8 -*-
import xadmin
from .models import *
from .forms import GoodForm, GoodMultimediaForm
from .resources import GoodResource, GoodMultimediaResource

class GoodAdmin(object):

    # 对象所有属性
    all_fields = [field.name for field in Good._meta.fields]
    all_fields.remove("id")
    # 对象 一对多 其他对象
    class GoodMultimediaInline(object):
        model = GoodMultimedia
        extra = 1
        style = 'tab'
        # exclude = ['', ]
    inlines = [GoodMultimediaInline, ]


    # 导入导出设置
    list_export = []  # 关闭默认导出插件
    import_export_args = {'import_resource_class': GoodResource, 'export_resource_class': GoodResource}  # 开启导入导出插件

    # 过滤 精确查询 字符 数值 日期
    # 搜索 模糊查询 字符
    # 通过计算的取值的字段不可过滤和搜索
    search_fields = ['title', 'info']  # 文本 模糊查询
    list_filter = [field.name for field in Good._meta.fields if field.name not in ["num"] ]

    # 列表页显示字段 list_display = '__all__' 会报错
    list_display = ['code','title','price_sell','price_purchase','good_purchase_num','good_sale_num','created']
    # 列表页可修改字段
    list_editable = ['title','price_sell', 'price_purchase']
    # 列表页详情预览
    show_detail_fields = ['code']  # 本身唯一标志和外键
    # 列表页进入详情业 入口字段
    list_display_links = ['title']
    # 列表页排序
    # ordering = ["-some_field","some_field"]
    # 列表页分页
    # list_per_page = 5

    # 详情页只读字段
    readonly_fields = ['created', 'modified']
    # 详情业显示字段与只读字段允许存在交叉
    # fields = []
    # 详情业隐藏字段与只读字段/显示字段互斥
    # exclude = []
    # 详情页字段输入输出样式
    relfield_style = 'fk-ajax'
    style_fields = {"info": "ueditor"}
    # 详情页表单定制
    form = GoodForm
    # 详情业排版
    # form_layout

    # 刷新时间 单位秒
    # refresh_times = [1, 60, 3600, 86400]

    # 导航图标
    # model_icon
    # 导航隐藏
    # hidden_menu


    def post(self,request, *args, **kwargs):
        return super(GoodAdmin, self).post(request, *args, **kwargs)


class GoodMultimediaAdmin(object):
    all_fields = [field.name for field in Good._meta.fields]

    list_export = []
    import_export_args = {'import_resource_class': GoodMultimediaResource, 'export_resource_class': GoodMultimediaResource}

    list_filter = ['type', 'fk_good__title', 'fk_good__code']
    search_fields = ['fk_good__title','fk_good__code']

    style_fields = {"info": "ueditor"}
    relfield_style = 'fk-ajax'
    readonly_fields = ['created', 'modified']
    form = GoodMultimediaForm

    list_display = ['fk_good', 'type', 'uri_image', 'uri']  # 可以直接填外键
    show_detail_fields = ['fk_good']
    list_display_links = ['fk_good']






xadmin.site.register(Good, GoodAdmin)
xadmin.site.register(GoodMultimedia, GoodMultimediaAdmin)

